#include "pageebtrigreason.h"
#include "ui_pageebtrigreason.h"

PageEBTrigReason::PageEBTrigReason(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageEBTrigReason)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_lEBTrigReasons << "由1车蘑菇按钮触发的紧急制动"
                << "由6车蘑菇按钮触发的紧急制动"
                << "由1车紧急停车按钮触发的紧急制动"
                << "由6车紧急停车按钮触发的紧急制动"
                << "由1车警惕按钮松开触发的紧急制动"
                << "由6车警惕按钮松开触发的紧急制动"
                << "由1车总风压力低触发的紧急制动"
                << "由6车总风压力低触发的紧急制动"
                << "由1车EBCB断开触发的紧急制动"
                << "由6车EBCB断开触发的紧急制动"
                << "由1车方向改变触发的紧急制动"
                << "由6车方向改变触发的紧急制动"
                << "由两端司机室均不激活触发的紧急制动"
                << "由超速触发的紧急制动"
                << "由1车障碍物及脱轨检测装置触发的紧急制动"
                << "由6车障碍物及脱轨检测装置触发的紧急制动"
                << "由1车ATP触发的紧急制动"
                << "由6车ATP触发的紧急制动"
                << "由1车紧急制动列车线断路器触发的紧急制动"
                << "由6车紧急制动列车线断路器触发的紧急制动"
                << "由不明原因触发的紧急制动"
                << "由两端司机室都激活触发的紧急制动";
    InitEBTrigLabels();
}

PageEBTrigReason::~PageEBTrigReason()
{
    delete ui;
}

void PageEBTrigReason::InitEBTrigLabels()
{
    m_nTopSpace = 100;
    m_nBottomSpace = 50;
    m_nHSpace = 50;
    m_nLabelLen = 480;
    m_nLabelHeight = 50;
    m_nVSpace = 10;

    uint bypassCnt = m_lEBTrigReasons.length();
    uint cols = bypassCnt%ROWSINCOL_EB?(bypassCnt/ROWSINCOL_EB+1):(bypassCnt/ROWSINCOL_EB);
    m_nSideSpace = (this->width() - cols*m_nLabelLen-(cols-1)*m_nHSpace)/2;
    m_nStartx = m_nSideSpace;
    m_nStarty = m_nTopSpace;

    for(int i=0;i<m_lEBTrigReasons.length();i++)
    {
        uint nCol = i/ROWSINCOL_EB;
        QLabel *ebtrig = new QLabel(this);
        ebtrig->setStyleSheet(StatLabelStyle_TB);
        ebtrig->setGeometry(m_nStartx+nCol*(m_nLabelLen+m_nHSpace),
                           m_nStarty+(i%ROWSINCOL_EB)*(m_nLabelHeight+m_nVSpace),
                           m_nLabelLen,m_nLabelHeight);
        ebtrig->setText(m_lEBTrigReasons.at(i));
        m_lEBTrigLabels.append(ebtrig);
        m_lEBRigStats.append(0);
    }
}

void PageEBTrigReason::setEBTrigStat(int index, int stat)
{
    if(index >= m_lEBTrigReasons.length())
        return;

    switch (stat) {
    case 0:
        m_lEBTrigLabels[index]->setStyleSheet(StatLabelStyle_TB);
        break;
    case 1:
        m_lEBTrigLabels[index]->setStyleSheet(LabelRedStyle_TB);
        break;
    default:
        m_lEBTrigLabels[index]->setStyleSheet(StatLabelStyle_TB);
        break;
    }
    m_lEBRigStats[index] = stat;
    if(m_lEBRigStats.contains(1))
        emit signal_EB_trig(true);
    else
        emit signal_EB_trig(false);
}

void PageEBTrigReason::on_Btn_Return_clicked()
{
    emit signal_go_back();
}
